const Mock = require("mockjs");
const Random = Mock.Random;
const mockPage = require("../../utils/mockPage.js");

function creatImglist() {
  const imglistnums = Random.natural(0, 5);
  const imglist = [];
  for (let index = 0; index < imglistnums; index++) {
    imglist.push({
      id: index,
      imgurl: Random.image("200x200", Random.color()),
      name: Mock.mock("@cname()"),
      price: Random.float(0.01, 100, 2, 2),
    });
  }
  return imglist;
}

// http://mockjs.com/examples.html
// 创建一个实体对象
function creatDetail() {
  const randomStr = Number(Math.random().toString().substr(3, 5) + Date.now()).toString(36);
  return Mock.mock({
    id: Mock.mock("@id()"),
    No: "No" + Random.integer(1, 99999),
    cname: Mock.mock("@id()") + Mock.mock("@cname()"),
    namesort: Mock.mock("@cname()"),
    name: Mock.mock("@cname()"),
    namelong: "" + Mock.mock("@cname()") + randomStr,
    time: Mock.mock('@date("yyyy-MM-dd")') + Random.time("HH:mm:ss"),
    time_HHmmss: Random.time("HH:mm:ss"),
    time_HHmm: Random.time("HH:mm"),
    data_yyyyMMdd: Mock.mock('@date("yyyy-MM-dd")'),
    data_MMdd: Mock.mock('@date("MM-dd")'),
    title: Random.ctitle(),
    csentence: Random.csentence(),
    cword: Random.cword(),
    email: Random.email(),
    ctitle: Random.ctitle(5, 10),
    permissions: ["CHN", "JPN", "FRA"],
    imglists: creatImglist(),
    cparagraph: Random.cparagraph(50, 100),
    address: Mock.mock("@county(true)"),
    imgurl: Random.image("200x200", Random.color()),
    status: Random.integer(0, 1),
    "num|1-100": 1,
    "number|1-10": 1,
    "boolean|1-2": true,
    price: Random.float(0.01, 100, 2, 2),
    aaguid: "@guid",
  });
}

function creatPageList(pageNo = 1, pageSize = 10, params = "") {
  const pageObj = mockPage.pageUtil(pageNo, pageSize);
  let { curPageNums, itemStartid, totalPage } = { ...pageObj };

  const lists = [];
  while (curPageNums > 0) {
    lists.push({ _No: pageNo * pageSize - curPageNums + 1, ...creatDetail() });
    itemStartid++;
    curPageNums--;
  }
  //   console.log(lists);

  const pageData = {
    pageNo: pageNo,
    pageSize: pageObj.pageSize,
    curPageNums: pageObj.curPageNums,
    totalPage,
    totalNums: pageObj.totalNums,
  };
  //   console.log(pageData);

  return {
    pageData,
    list: lists,
  };
}

function creatList(pageNum = 1, pageSize = 10, params = "") {
  return [];
}

let imglist = [];
let detail = {};
let lists = [];
let pageList = {
  pageData: {},
  list: [],
};

//  初始化 Mock 数据
console.time("初始化 Mock 数据");
imglist = creatImglist();
detail = creatDetail();
lists = creatList();
pageList = creatPageList();
//  初始化 Mock 数据
console.timeEnd("初始化 Mock 数据");
console.info("http://localhost:5555/");
console.info("http://localhost:5555/swagger-ui.html");
console.info("http://localhost:5555/public/test.html");

// console.log("-----imglist------", imglist);
// console.log("-----detail------", detail);
// console.log("-----lists------", lists);
// console.log("-----pageList------", pageList);

module.exports = {
  detail,
  lists,
  imglist,
  pageList,

  creatImglist,
  creatDetail,
  creatPageList,
  creatList,
};
